package com.one.group.admin.controller;


import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.convert.Convert;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.one.group.enums.ErrorCode;
import com.one.group.model.dto.BaseDeleteDto;
import com.one.group.model.dto.InventoryRefinementQueryDto;
import com.one.group.model.entity.InventoryRefinement;
import com.one.group.model.vo.InventoryRefinementListVo;
import com.one.group.result.Result;
import com.one.group.service.InventoryRefinementService;
import com.one.group.utils.ResultUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("inventoryRefinement")
@Tag(name = "库存细化管理接口")
public class InventoryRefinementController {
    @Autowired
    InventoryRefinementService inventoryRefinementService;
    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;
    @PostMapping("list")
    @Operation(summary = "查询库存细化列表")
    public Result list(@RequestBody InventoryRefinementQueryDto inventoryRefinementQueryDto){
        Long tenantId = Convert.toLong(StpUtil.getExtra("tenant_id"));
        inventoryRefinementQueryDto.setTenantId(tenantId);
        PageHelper.startPage(inventoryRefinementQueryDto.getPage(),inventoryRefinementQueryDto.getLimit());
        List<InventoryRefinementListVo> inventoryRefinementListVos = inventoryRefinementService.findInventoryRefinementByQueryDto(inventoryRefinementQueryDto);
        PageInfo<InventoryRefinementListVo> pageInfo = new PageInfo<>(inventoryRefinementListVos);
        return ResultUtil.success(pageInfo);
    }

    @GetMapping("/find")
    @Operation(summary = "根据id查询库存细化信息")
    public Result find(@RequestParam Long id){
        return ResultUtil.success(inventoryRefinementService.getById(id));
    }
    @GetMapping("findAll")
    @Operation(summary = "查询所有库存细化信息")
    public Result findAll() {
        return ResultUtil.success(inventoryRefinementService.list());
    }
    @GetMapping("/delete")
    @Operation(summary = "删除库存细化信息")
    public Result delete(@RequestParam Long id){
        if (inventoryRefinementService.removeById(id))
            return ResultUtil.success();
        else return ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }
    @PostMapping("/add")
    @Operation(summary = "添加库存细化信息")
    public Result add(@RequestBody InventoryRefinement inventoryRefinement){

        if (inventoryRefinementService.save(inventoryRefinement)) return ResultUtil.success();
        else return ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }
    @PostMapping("/update")
    @Operation(summary = "修改库存细化信息")
    public Result update(@RequestBody InventoryRefinement inventoryRefinement){

        if (inventoryRefinementService.updateById(inventoryRefinement)) return ResultUtil.success();
        else return ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }
    @PostMapping("/deleteBatch")
    @Operation(summary = "批量删除库存细化信息")
    public Result deleteBatch(@RequestBody BaseDeleteDto baseDeleteDto){
        if (inventoryRefinementService.removeByIds(baseDeleteDto.getIds())) return ResultUtil.success();
        else return ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }

}
